package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    /**
     *
     * 最近一年的会员数量
     *  {
             months： [2019-08,2019-09],
             memberCount: [1, 6]
         }
     * @return
     */
    @Override
    public Map<String, Object> getMemberReport() {

        List<String> months = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();

        //后退12个月
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            Date date = calendar.getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
            String month = dateFormat.format(date);
            months.add(month);
            //前进一个月
            calendar.add(Calendar.MONTH, 1);
        }
        List<Long> memberCount = new ArrayList<>();
        //memberCount[]
        //每个月都需要查询会员的总数： 查询每月最后一天之前的会员总数
        for (String month : months) {
            //每个月的最后一天
            month += "-31";// month = month + "-31"
            long count = memberDao.findByMonth(month);
            memberCount.add(count);
        }

        Map<String ,Object> map = new HashMap<>();
        map.put("months",months );
        map.put("memberCount",memberCount );

        return map;
    }

    @Override
    public Map<String, Object> getMemberReportByDate(String start,String end) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
            bef.setTime(sdf.parse(start));
            aft.setTime(sdf.parse(end));


        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;

            int abs = Math.abs(month + result);

            List<String> months = new ArrayList<>();




        //后退12个月
        aft.add(Calendar.MONTH, -abs);
        for (int i = 0; i <= abs; i++) {
            Date date = bef.getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
            String month1 = dateFormat.format(date);
            months.add(month1);
            //前进一个月
            bef.add(Calendar.MONTH, 1);
        }
        List<Long> memberCount = new ArrayList<>();
        //memberCount[]
        //每个月都需要查询会员的总数： 查询每月最后一天之前的会员总数
        for (String month2 : months) {
            //每个月的最后一天
            month2 += "-31";// month = month + "-31"
            long count = memberDao.findByMonth(month2);
            memberCount.add(count);
        }
        Map<String ,Object> map = new HashMap<>();
        map.put("months",months );
        map.put("memberCount",memberCount );

        return map;
    }

    @Override
    public Map<String, Object> findBySex() {

        Map<String, Object> stringObjectMap = new HashMap<String, Object>();

            List<String> sexNames = new ArrayList<>();
            List<Map<String,String>> sexCount=memberDao.findByCountSex();

        for (Map<String, String> map : sexCount) {

            String name = map.get("name");
            if (name.equals("1")){
                name="男";
                map.put("name",name);
            }else if (name.equals("2")){
                name="女";
                map.put("name",name);
            }

            sexNames.add(name);
        }
        stringObjectMap.put("memberSex",sexNames);
        stringObjectMap.put("membernum",sexCount);

        return stringObjectMap;
    }

    @Override
    public Map<String, Object> findByAge() {
        Map<String, Object> stringObjectMap = new HashMap<String, Object>();

        List<String> ageNames = new ArrayList<>();
        List<Member> ageCount=memberDao.findByCountAge();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");

        ageNames.add("1-10");
        ageNames.add("10-20");
        ageNames.add("20-30");
        ageNames.add("30-40");

        Calendar c1 = Calendar.getInstance();   //当前日期
        Calendar c2 = Calendar.getInstance();

        int year = c1.get(Calendar.YEAR);
        List<Map<String,String>> ageMap = new ArrayList<>();
        int j1 = 0;
        int j2 = 0;
        int j3 = 0;
        int j4 = 0;
        for (int i = 0; i < ageCount.size(); i++) {
            Member member = ageCount.get(i);
            Date birthday = member.getBirthday();
            c2.setTime(birthday);
            int oldYear = c2.get(Calendar.YEAR);
            int age = year-oldYear;
            HashMap<String, String> stringStringHashMap = new HashMap<>();






            if (age>1 && age<10){
                stringStringHashMap.put("name","1-10");
                stringStringHashMap.put("value",String.valueOf(j1));
//                Object o = stringObjectMap.get("1-10");
                j1++;
                String  j1s = String.valueOf(j1);

                stringStringHashMap.put("value",j1s);
            }
            if (age>10 && age<20){
                stringStringHashMap.put("name","10-20");
                stringStringHashMap.put("value",String.valueOf(j2));
                j2++;
                String  j2s = String.valueOf(j2);
                stringStringHashMap.put("value",j2s);
            }
            if (age>20 && age<30){
                stringStringHashMap.put("name","20-30");
                stringStringHashMap.put("value",String.valueOf(j3));
                j3++;
                String  j3s = String.valueOf(j3);
                stringStringHashMap.put("value",j3s);
            }
            ageMap.add(stringStringHashMap);
        }


        stringObjectMap.put("memberAge",ageNames);
        stringObjectMap.put("memberAgeCount",ageMap);

        return stringObjectMap;
    }
}
